﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL.TuyenDung
{
    /// <summary>
    /// tinhtrang: 0 -> ban tam chua hoan thanh, tinhtrang: 1 -> hoan thien da duyet, 2 -> huy bo an
    /// LoaiCongViecId: 0 ->Toàn thời gian cố định(full-time) ; 1 -> Toàn thời gian tạm thời ; 2 -> Bán thời gian(part-time) ; 3 ->Bán thời gian tam thoi ; 4 -> thuc tap ; 5 -> khac
    /// cachhienthi: 0 -> binh thuong, cachHienThi: 1 -> chu do
    /// phuongthucthanhtoan: 0 -> mien phi, 1 -> tien mat, 2 -> ngan hang
    /// gioitinh: 0 -> nam/nu ;1 -> nam ; 2 -> nu
    /// cachlienhe:1;2;3 ->nop truc tuyen,Nop truc tiep, email, 
    /// </summary>
    public class DangTuyenHelper
    {
        public static void UpDataTrangThaiDaDocDonTuyenDung(int tuyendungid,int hosoid)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var temp = db.tbl_TD_NopTuyenDungs.FirstOrDefault(k => k.TuyenDungId == tuyendungid && k.HoSoId == hosoid);
            if(temp!=null)
            {
                temp.DaDoc = true;
                db.SubmitChanges();
            }
        }
        public static int DangTuyenDungBuoc1(int userId, string chucDanh, string tenCongTy, string soLuocCongTy, string soNhanVien, string website)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_TD_TuyenDung tuyenDung = new tbl_TD_TuyenDung();
            tuyenDung.UserId = userId;
            tuyenDung.ChucDanh = chucDanh;
            tuyenDung.URL = Util.VietnameseText.ToUpText(chucDanh);
            tuyenDung.TenCongTy = tenCongTy;
            tuyenDung.SoLuocCongTy = soLuocCongTy;
            tuyenDung.SoNhanVien = soNhanVien;
            tuyenDung.Website = website;
            tuyenDung.TinhTrang = 0;
            tuyenDung.NgayTao = DateTime.Now;
            tuyenDung.NgayDang = DateTime.Now;
            tuyenDung.SoLuotXem = 0;
            tuyenDung.Step = 1;
            tuyenDung.NgayHetHan = DateTime.Now.AddDays(30);
            db.tbl_TD_TuyenDungs.InsertOnSubmit(tuyenDung);
            db.SubmitChanges();
            int id = tuyenDung.TuyenDungId;
            return id;

        }
        public static void UpdateBuoc1(int TuyenDungid, string chucDanh, string tenCongTy, string soLuocCongTy, string soNhanVien, string website)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var tuyenDung = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungid);
            tuyenDung.ChucDanh = chucDanh;
            tuyenDung.URL = Util.VietnameseText.ToUpText(chucDanh);
            tuyenDung.TenCongTy = tenCongTy;
            tuyenDung.SoLuocCongTy = soLuocCongTy;
            tuyenDung.SoNhanVien = soNhanVien;
            tuyenDung.Website = website;
            db.SubmitChanges();
        }
        public static void DangTuyenDungBuoc2(int TuyenDungid, int? nganhNgheid1, int? nganhNgheid2, int? nganhNgheid3, string noiLamViec, int? LoaiCongViec, int? CapBacCVId, string Luong, string Tuoi, int? GioiTinh, string MoTaCongViec, int? HocVanid, string MucDoKinhNghiem, string KyNangChiTiet)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var dangtuyendung = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungid);
            if (dangtuyendung != null)
            {
                dangtuyendung.NganhNgheId1 = nganhNgheid1;
                dangtuyendung.NganhNgheId2 = nganhNgheid2;
                dangtuyendung.NgheNgheId3 = nganhNgheid3;
                dangtuyendung.NoiLamViec = noiLamViec;
                dangtuyendung.LoaiCongViecId = LoaiCongViec;
                dangtuyendung.CapBacCVId = CapBacCVId;
                dangtuyendung.Luong = Luong;
                dangtuyendung.Tuoi = Tuoi;
                dangtuyendung.GioiTinh = GioiTinh;
                dangtuyendung.MoTaCongViec = MoTaCongViec;
                dangtuyendung.HocVanId = HocVanid;
                dangtuyendung.MucKinhNghiem = MucDoKinhNghiem;
                dangtuyendung.KinhNghiemChiTiet = KyNangChiTiet;
                dangtuyendung.Step = 2;
                db.SubmitChanges();
            }
        }
        public static void DangTuyenDungBuoc3(int TuyenDungid, string CachLienHe, int? NgonNguNhanHoSo, string Mota, string DiaChi, int? QuocGiaId, int? TinhId, string TenLienHe, string DienThoai, string Fax, string EmailLienHe, string NguoiLienHe, DateTime NgayDang, DateTime NgayHetHan, int CachHienThi, int PhuongThucThanhToan)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var dangtuyendung = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungid);
            if (dangtuyendung != null)
            {
                dangtuyendung.CachLienHe = CachLienHe;
                dangtuyendung.NgonNguHoSo = NgonNguNhanHoSo;
                dangtuyendung.TTLHMoTa = Mota;
                dangtuyendung.DiaChi = DiaChi;
                dangtuyendung.TinhId = TinhId;
                dangtuyendung.QuocGiaId = QuocGiaId;
                dangtuyendung.TenLienHe = TenLienHe;
                dangtuyendung.DienThoai = DienThoai;
                dangtuyendung.Fax = Fax;
                dangtuyendung.EmailLienHe = EmailLienHe;
                dangtuyendung.NguoiLienHe = NguoiLienHe;
                dangtuyendung.NgayDang = NgayDang;
                dangtuyendung.NgayHetHan = NgayHetHan;
                dangtuyendung.Step = 3;
                dangtuyendung.CachHienThi = CachHienThi;
                dangtuyendung.PhuongThucThanhToan = PhuongThucThanhToan;
                db.SubmitChanges();
            }
        }
        public static tbl_TD_TuyenDung Get_TinTuyenDung(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungId);
        }
        public static string NoiLamViec(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            string NoiLamViec = "";
            try
            {
                string[] chuoi = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungId).NoiLamViec.Split(';');
                foreach (var s in chuoi)
                {
                    NoiLamViec += BLL.Admin.TinhHelper.Get_TinhId(int.Parse(s)).Ten + "; ";
                }
            }
            catch
            {

            }

            return NoiLamViec;
        }
        public static IQueryable List_TinTuyenDungCuaToi(int userId, int NganhNgheId, int tinhTrang, int Page, int row)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            if (tinhTrang != 3)
            {
                var query = from b in db.tbl_TD_TuyenDungs.Where(p => p.UserId == userId && (NganhNgheId == -1 || p.NganhNgheId1 == NganhNgheId || p.NganhNgheId2 == NganhNgheId || p.NgheNgheId3 == NganhNgheId) && (tinhTrang == -1 || p.TinhTrang == tinhTrang))
                            orderby b.NgayTao descending
                            select new
                            {
                                b.TuyenDungId,
                                b.ChucDanh,
                                b.TenCongTy,
                                DiaDiem = BLL.TuyenDung.DangTuyenHelper.NoiLamViec(b.TuyenDungId),
                                b.SoLuotXem,
                                b.NgayTao,
                                b.NgayDang,
                                b.NgayHetHan,
                                TrangThai = b.TinhTrang == 0 ? "Bản tạm" : (b.TinhTrang == 1 ? "Đã đăng" : "Ẩn")
                            };
                return query.Skip(row * (Page - 1)).Take(row);
            }
            else // viejc lam het han
            {
                var query = from b in db.tbl_TD_TuyenDungs.Where(p => p.UserId == userId && (NganhNgheId == -1 || p.NganhNgheId1 == NganhNgheId || p.NganhNgheId2 == NganhNgheId || p.NgheNgheId3 == NganhNgheId) && p.NgayHetHan < DateTime.Now)
                            orderby b.NgayTao descending
                            select new
                            {
                                b.TuyenDungId,
                                b.ChucDanh,
                                b.TenCongTy,
                                DiaDiem = BLL.TuyenDung.DangTuyenHelper.NoiLamViec(b.TuyenDungId),
                                b.SoLuotXem,
                                b.NgayTao,
                                b.NgayDang,
                                b.NgayHetHan,
                                TrangThai = "Hết hạn",
                            };
                return query.Skip(row * (Page - 1)).Take(row);
            }
        }
        public static int CountList_TinTuyenDungCuaToi(int userId, int NganhNgheId, int tinhTrang)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            if (tinhTrang != 3)
            {
                var query = from b in db.tbl_TD_TuyenDungs.Where(p => p.UserId == userId && (NganhNgheId == -1 || p.NganhNgheId1 == NganhNgheId || p.NganhNgheId2 == NganhNgheId || p.NgheNgheId3 == NganhNgheId) && (tinhTrang == -1 || p.TinhTrang == tinhTrang))
                            select new
                            {
                                b
                            };
                return query.Count();
            }
            else // viejc lam het han
            {
                var query = from b in db.tbl_TD_TuyenDungs.Where(p => p.UserId == userId && (NganhNgheId == -1 || p.NganhNgheId1 == NganhNgheId || p.NganhNgheId2 == NganhNgheId || p.NgheNgheId3 == NganhNgheId) && p.NgayHetHan < DateTime.Now)

                            select new
                            {
                                b
                            };
                return query.Count();
            }
        }
        public static void Update_TinhTrang(int TuyenDungid, int TinhTrangId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var tuyenDung = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungid);
            if (tuyenDung != null)
            {
                tuyenDung.TinhTrang = TinhTrangId;
                db.SubmitChanges();
            }
        }
        public static bool Delete_TinTuyenDung(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            try
            {
                var tuyenDung = db.tbl_TD_TuyenDungs.FirstOrDefault(k => k.TuyenDungId == TuyenDungId);
                if (tuyenDung != null)
                {
                    db.tbl_TD_TuyenDungs.DeleteOnSubmit(tuyenDung);
                    db.SubmitChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static IQueryable List_CVNoiBat_LoaiCV(int LoaiCongViecId, int take)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tuyendung in db.tbl_TD_TuyenDungs.Where(k => k.LoaiCongViecId == LoaiCongViecId && k.TinhTrang == 1)
                        orderby tuyendung.SoLuotXem descending
                        select new
                                   {
                                       tuyendung.TuyenDungId,
                                       tuyendung.URL,
                                       tuyendung.ChucDanh,
                                       tuyendung.TenCongTy,
                                       css = tuyendung.CachHienThi == 1 ? "top" : "",
                                   };
            return query.Take(take);
        }
        public static IQueryable List_CVNoiNhat_LoaiCV(int LoaiCongViecId, int take)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tuyendung in db.tbl_TD_TuyenDungs.Where(k => k.LoaiCongViecId == LoaiCongViecId && k.TinhTrang == 1 && k.NgayDang >= DateTime.Now)
                        orderby tuyendung.NgayDang descending
                        select new
                        {
                            tuyendung.TuyenDungId,
                            tuyendung.ChucDanh,
                            tuyendung.URL,
                            tuyendung.TenCongTy,
                            css = tuyendung.CachHienThi == 1 ? "top" : "",
                        };
            return query;
        }
        public static IQueryable List_ViecLamHapDan(int tak)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tuyendung in db.tbl_TD_TuyenDungs.Where(k => k.TinhTrang == 1 && k.NgayDang >= DateTime.Now)
                        orderby tuyendung.SoLuotXem descending
                        select new
                        {
                            tuyendung.TuyenDungId,
                            tuyendung.ChucDanh,
                            tuyendung.TenCongTy,
                            tuyendung.URL,
                        };
            return query.Take(tak);
        }
        public static IQueryable List_Top_ViecLamDaDang(int tak)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tuyendung in db.tbl_TD_TuyenDungs.Where(k => k.TinhTrang == 1 && k.NgayDang >= DateTime.Now)
                        orderby tuyendung.NgayDang descending
                        select new
                        {
                            tuyendung.TuyenDungId,
                            tuyendung.ChucDanh,
                            tuyendung.TenCongTy,
                            tuyendung.URL,
                        };
            return query.Take(tak);
        }
        public static IQueryable List_TinTuyenDungAll(int userid)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from a in db.tbl_TD_TuyenDungs.Where(k => k.UserId == userid)
                        orderby a.NgayDang descending
                        select new
                                   {
                                       a.TuyenDungId,
                                       a.ChucDanh,
                                       a.URL,
                                   };
            return query;
        }
        public static int SoCV_DaDang()
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tuyendung in db.tbl_TD_TuyenDungs.Where(k => k.TinhTrang == 1 && k.NgayDang >= DateTime.Now)
                        select tuyendung;
            return query.Count();
        }
        public static IQueryable List_TinTuyenDungTrangTaiKhoan(int userId, int take)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from b in db.tbl_TD_TuyenDungs.Where(p => p.UserId == userId)
                        orderby b.NgayTao descending
                        select new
                        {
                            b.TuyenDungId,
                            b.ChucDanh,
                            b.TenCongTy,
                            DiaDiem = BLL.TuyenDung.DangTuyenHelper.NoiLamViec(b.TuyenDungId),
                            b.SoLuotXem,
                            b.NgayTao,
                            b.NgayDang,
                            b.NgayHetHan,
                            TrangThai = b.TinhTrang == 0 ? "Bản tạm" : (b.TinhTrang == 1 ? "Đã đăng" : "Ẩn")
                        };
            return query.Take(take);

        }
        //
        public static int SoHoSoDaDoc(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_TD_NopTuyenDungs.Where(k => k.TuyenDungId == TuyenDungId && k.DaDoc == true).Count();
        }
        public static int SoHoSoChuaDoc(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_TD_NopTuyenDungs.Where(k => k.TuyenDungId == TuyenDungId && k.DaDoc == false).Count();
        }
        public static int TongSoHoSoDaNhan(int TuyenDungId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            return db.tbl_TD_NopTuyenDungs.Where(k => k.TuyenDungId == TuyenDungId).Count();
        }
        public static IQueryable List_UngVienCuaToi(int TuyenDungId, bool tinhTrang, int Page, int row, bool all)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from b in db.tbl_TD_NopTuyenDungs.Where(p => (p.TuyenDungId == TuyenDungId || TuyenDungId==-1)&& (p.DaDoc == tinhTrang || all))
                        orderby b.tbl_TD_TuyenDung.NgayDang descending
                        select new
                        {
                            b.TuyenDungId,
                            b.tbl_TD_TuyenDung.ChucDanh,
                            b.NgayGui,
                            b.tbl_HS_HoSo.User.UserName,
                            TrangThai = b.DaDoc == true ? "Đã xem" : "Chưa xem",
                            Email = b.tbl_HS_HoSo.Email,
                            id =b.HoSoId+"-"+b.TuyenDungId,
                        };
            return query.Skip(row * (Page - 1)).Take(row);
        }
        public static int CountList_List_UngVienCuaToi(int TuyenDungId, bool tinhTrang, bool all)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query =
                from b in
                    db.tbl_TD_NopTuyenDungs.Where(p => (p.TuyenDungId == TuyenDungId || TuyenDungId == -1) && (p.DaDoc == tinhTrang || all))
                select b;
            return query.Count();

        }
    }
}